package com.ycx.ycxmybatismanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ycx.ycxmybatismanagement.dto.StudentDTO;
import com.ycx.ycxmybatismanagement.dto.StudentListDTO;
import com.ycx.ycxmybatismanagement.mapper.StudentMapper;
import com.ycx.ycxmybatismanagement.pojo.PageBean;
import com.ycx.ycxmybatismanagement.pojo.Student;
import com.ycx.ycxmybatismanagement.service.StudentService;
import com.ycx.ycxmybatismanagement.vo.StudentListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageBean<StudentListVO> studentPageList(StudentListDTO studentListDTO) {
        PageHelper.startPage(studentListDTO.getPage(),studentListDTO.getPageSize());
        Page<StudentListVO> studentPageList = studentMapper.studentPageList(studentListDTO);
        return new PageBean(studentPageList.getTotal(),studentPageList.getResult());
    }

    @Override
    public void deleteStudentListByIds(Integer[] ids) {
        studentMapper.deleteStudentListById(ids);
    }

    /**
     * 新增
     * @param studentDTO
     */
    @Override
    public void addStudent(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO,student);
        student.setCreateTime(LocalDate.now());
        student.setUpdateTime(LocalDate.now());
        studentMapper.addStudent(student);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @Override
    public StudentListVO selectStudentById(Integer id) {
        StudentListVO studentVo =  studentMapper.selectStudentById(id);
        return studentVo;
    }

    /**
     * 修改
     * @param studentDTO
     */
    @Override
    public void updateStudentById(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO,student);
        studentMapper.updateStudentById(student);
    }

    @Override
    public void updateStudentScore(Integer id,Integer scores) {
        Integer count = studentMapper.selectStudentCountById(id);
        Integer score = studentMapper.selectStudentScoreById(id);
        Student student = new Student();
        student.setId(id);
        student.setViolationCount(count+1);
        student.setViolationScore(scores+score);
        studentMapper.updateStudentScore(student);
    }
}
